package com.wangsd.web.shop.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.basicbean.excel.ExcelData;
import com.wangsd.common.basicbean.excel.POIUtil;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.web.shop.entity.Coupon;
import com.wangsd.web.shop.entity.CouponOrder;
import com.wangsd.web.shop.entity.Trade;
import com.wangsd.web.shop.service.CouponOrderService;
import com.wangsd.web.shop.service.CouponService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "代金券")
@RestController
@RequestMapping("/shop/couponOrder")
@Slf4j
public class CouponOrderController extends MyController {

    @Autowired
    private CouponOrderService service;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             @ApiParam(value = "排序字段") @RequestParam(required = false) String sortField,
                             @ApiParam(value = "排序方式") @RequestParam(required = false) String sortOrder,
                             CouponOrder queryObj) {
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        if (StringUtils.isBlank(sortField)) {
            sortField = "consumeTime";
        }
        if (StringUtils.isBlank(sortOrder)) {
            sortOrder = "descend";
        }
        List<CouponOrder> list = service.selectPage(queryObj, sortField, sortOrder, loginUser.getParentCode());
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "导出核销记录")
    @PostMapping("/exportExcel")
    public void exportExcel(String stockId) {
        List<CouponOrder> list = service.selectByStockId(stockId);
        ExcelData excelData = new ExcelData();
        List<String> titles = new ArrayList<>();
        ArrayList<List<Object>> rowList = new ArrayList<>();//行
        titles.add("核销商户名称");
        titles.add("批次号");
        titles.add("批次名称");
        titles.add("会员昵称");
        titles.add("会员openid");
        titles.add("领取时间");
        titles.add("核销时间");
        titles.add("核销商户号");
        titles.add("核销订单号");
        titles.add("商户订单号");
        excelData.setTitles(titles);
        for (CouponOrder order : list) {
            List<Object> strList = new ArrayList<>();
            strList.add(order.getMerchantName());
            strList.add(order.getStockId());
            strList.add(order.getStockName());
            strList.add(order.getNickName());
            strList.add(order.getMiniOpenid());
            strList.add(DateUtils.formatDatetime(order.getCreatedTime()));
            strList.add(order.getConsumeTime());
            strList.add(order.getConsumeMchid());
            strList.add(order.getTransactionId());
            strList.add(order.getOutTradeNo());
            rowList.add(strList);
        }
        excelData.setRows(rowList);
        try {
            POIUtil.exportExcel(response, stockId + ".xls", excelData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
